package com.jfm.jfmc.core.constants;

import com.jfm.jfmm.collector.enums.AlarmStatus;


/**
 * @author DYang
 * @since 2024-11-07 0:40
 *
 * TDengine SQL 常量
 */
public interface MonitorMetricsSQL {

    /**
     * 创建数据库,并设置默认缓存，
     * 缓存128页，每页32kb ,总计4M
     */
    String CREATE_DB_SQL = "CREATE DATABASE IF NOT EXISTS %s PAGES 128 PAGESIZE 32;";

    /**
     * 创建超级表
     */
    String CREATE_SUPER_TABLE_SQL =
            """
            CREATE STABLE IF NOT EXISTS %s.monitor_metrics (
                ts timestamp,
                alarm INT,
                response_time INT,
                metric_code BINARY(64),
                collect_code BINARY(64),
                data Float,
                unit BINARY(32)
            ) TAGS (
                device_code BINARY(64)
            );
            """;

    /**
     * 创建子表
     */
    String CREATE_TABLE_SQL = """
            insert into ${childTable}
            using monitor_metrics(deviceCode,metricCode,unit,collectCode,jsonStr)
            tags (${tagValues})
            values (${ts},${value},${alarm},${responseTime});
            """;


    /**
     * 获取创建数据库的sql
     * @param dbName 数据库名称
     * @return sql
     */
    static String getCreateDbSql(String dbName) {
        return String.format(CREATE_DB_SQL, dbName);
    }

    /**
     * 获取创建超级表的sql
     * @return sql
     */
    static String getCreateSuperTableSql(String databaseName) {
        return String.format(CREATE_SUPER_TABLE_SQL, databaseName);
    }


    /**
     * 获取创建子表的sql
     * @param childTable 子表名称
     * @param tagValues 标签值
     * @param ts 时间戳
     * @param value 值
     * @param alarm 告警状态
     * @return sql
     */
    static String getChildTableSql(String childTable, String[] tagValues, Long ts,Long duration, Object value, Integer alarm) {
        String res = "";
        for (String tagValue : tagValues) {
            if (tagValue == null) {
                res += "," +  null;
            }else {
                res += ",'" + tagValue + "'";
            }
        }
        res = res.substring(1);
//        String tagValueParams = "'" +  String.join("','", tagValues);
        ts = ts == null ? System.currentTimeMillis() : ts;
        duration = duration == null ? 0 : duration;
        value = value == null ? 0f : value;
        alarm = alarm == null ? AlarmStatus.ALARM_STATUS_NORMAL.getCode() : alarm;

        return CREATE_TABLE_SQL.replace("${childTable}", childTable)
                .replace("${tagValues}", res)
                .replace("${ts}", String.valueOf(ts))
                .replace("${value}", String.valueOf(value))
                .replace("${alarm}", String.valueOf(alarm))
                .replace("${responseTime}", String.valueOf(duration));
    }

    /**
     * 获取创建子表的sql
     * @param childTable 子表名称
     * @param tagValues 标签值
     * @return sql
     */
    static String getChildTableSql(String childTable, String[] tagValues) {
        return getChildTableSql(childTable, tagValues,null,0L, null, null);
    }
}
